package com.zpark.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @PackageName: com.zpark.config
 * @ClassName: WebInit
 * @Description:
 * @author: RZS
 * @date: 2023/4/17  14:39
 */
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

    // 设置 Spring 的配置类
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    // 设置 Spring MVC 的配置类
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMVCConfig.class};
    }

    // 为 DispatcherServlet 指定映射规则，相当于 web.xml 中配置的 url-pattern
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    // 添加过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        // 设置请求编码
        encodingFilter.setEncoding("UTF-8");
        // 设置响应编码
        encodingFilter.setForceResponseEncoding(true);

        // 来处理 PUT 和 DELETE 请求的过滤器
        HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();

        return new Filter[]{encodingFilter, methodFilter};
    }
}
